﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Code
{
    public static class Help
    {
        /// <summary>
        /// 计算byteArray大小
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        public static string BytesToSize(Byte[] byteArray)
        {
            return BytesCountToString(byteArray.Length);
        }

        /// <summary>
        /// 返回存储单位
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string BytesCountToString(double len)
        {
            string[] sizes = { "字节", "KB", "MB", "GB", "TB" };
            int order = 0;
            while (len >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                len = len / 1024;
            }
            if (order == 0)
                return String.Format("{0:0.##}{1}", len.ToString("N0"), sizes[order]);
            else if (order > 2)
                return String.Format("{0:0.##}{1}", len.ToString("N" + order), sizes[order]);
            else
                return String.Format("{0:0.##}{1}", len.ToString("N2"), sizes[order]);
        }
    }
}
